# -*- coding: utf8 -*-

import logging
from os import path
from .io import ImageFile, LabelFile

logger = logging.getLogger(__name__)


def read_data_set(prefix, mapper=None):
    logger.debug('Reading images...')
    image_file = ImageFile(path.dirname(__file__) + '/../data/%s-images-idx3-ubyte' % prefix)
    label_file = LabelFile(path.dirname(__file__) + '/../data/%s-labels-idx1-ubyte' % prefix)
    logger.debug('Done reading images.')
    logger.info('Number of vectors: %d.' % image_file.numImages)

    logger.debug('Preparing training vectors...')
    px_gen = image_file.images()
    if mapper:
        px_gen = map(mapper, px_gen)
    px_vec = [v.flatten() for v in px_gen]
    lb_vec = list(label_file.labels())
    logger.debug('Done preparing training vectors.')

    return px_vec, lb_vec

